/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.user;

import com.workcell.workdata.im.annotation.ClientOrigin;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.annotation.UpdateAction;
import com.workcell.workdata.im.component.cim.pusher.DefaultMessagePusher;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Friend;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.request.FriendAcceptRequest;
import com.workcell.workdata.im.model.request.FriendAlisaRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.UserVO;
import com.workcell.workdata.im.service.FriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/friend")
@Api(produces = "application/json", tags = "好友相关接口")
@Validated
public class FriendController {

	@Resource
	private FriendService friendService;

	@Resource
	private DefaultMessagePusher defaultMessagePusher;

	@ApiOperation(httpMethod = "GET", value = "获取好友列表")
	@GetMapping(value = "/list")
	public ResponseEntity<List<UserVO>> list(@ApiIgnore @UID Long uid) {
		return ResponseEntity.ok(friendService.findList(uid));
	}

	@ApiOperation(httpMethod = "POST", value = "同意添加好友")
	@ApiImplicitParam(name = "fid", value = "用户ID", paramType = "path", dataTypeClass = Long.class, required = true, example = "0")
	@PostMapping(value = "/agree/{fid}")
	public ResponseEntity<Void> agree(@PathVariable Long fid, @ApiIgnore @UID Long uid) {

		Friend friend = new Friend();
		friend.setUid(uid);
		friend.setFid(fid);

		friendService.add(friend);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "同意添加好友(new)")
	@PostMapping(value = "/accept")
	public ResponseEntity<Void> accept(@ApiIgnore @UID Long uid,@RequestBody FriendAcceptRequest request) {

		Friend friend = new Friend();
		friend.setUid(uid);
		friend.setFid(request.getUid());
		friend.setAlias(request.getAlias());

		friendService.add(friend);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "申请添加好友")
	@ApiImplicitParam(name = "fid", value = "用户ID", paramType = "path", dataTypeClass = Long.class, required = true, example = "0")
	@ApiResponses(value = {@ApiResponse(responseCode = "200" ,description = "OK"),@ApiResponse(responseCode = "423" ,description = "已经是好友")})
	@PostMapping(value = "/apply/{fid}")
	public ResponseEntity<Void> apply(@PathVariable Long fid,
									  @ApiIgnore @UID Long uid,
									  @ApiIgnore @ClientOrigin Origin origin ,
									  @RequestParam String message) {

		if (friendService.isFriend(uid,fid)){
			return ResponseEntity.make(HttpStatus.LOCKED,"已经是好友");
		}

		Message eventMessage = new EventMessage();
		eventMessage.setFormat(MessageFormat.TEXT);
		eventMessage.setReceiver(fid);
		eventMessage.setAction(MessageAction.ACTION_105);
		eventMessage.setSender(uid);
		eventMessage.setContent(message);

		defaultMessagePusher.push(eventMessage,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除好友")
	@ApiImplicitParam(name = "fid", value = "用户ID", paramType = "path", dataTypeClass = Long.class, required = true, example = "0")
	@DeleteMapping(value = "/{fid}")
	public ResponseEntity<Void> delete(@PathVariable Long fid, @ApiIgnore @UID Long uid) {
		friendService.delete(uid,fid);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "设置好友备注")
	@PostMapping(value = "/alias")
	@Validated(UpdateAction.class)
	public ResponseEntity<Void> alias(@ApiIgnore @UID Long uid,@Valid @RequestBody FriendAlisaRequest request) {
		friendService.setAlias(uid,request);
		return ResponseEntity.ok();
	}
}
